#include "savedqueriesdlg.h"
#include <QtGui>
SavedQueriesDlg::SavedQueriesDlg():QDialog()
{
	setupUi(this);
	
	QStandardItemModel *model = new QStandardItemModel();
	tableQueries->setModel(model);
	
	QStringList slist = savedQueries();
	model->clear();
	foreach(QString q,slist){		
		model->appendRow(new QStandardItem(q));		
	}
	tableQueries->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents);
	tableQueries->horizontalHeader()->hide();
}


QStringList SavedQueriesDlg::savedQueries()
{
	QStringList slist;
	QSettings sets;
	int count = sets.beginReadArray("queries");
	for(int i=0;i<count;i++){
		sets.setArrayIndex(i);
		slist << sets.value("q","").toString();
	}
	sets.endArray();	
	return slist;
}

void SavedQueriesDlg::addQuery(const QString &query)
{
	QStringList slist = savedQueries();
	if(slist.indexOf(query)>=0) return;
	slist << query;	
	QSettings sets;
	sets.beginWriteArray("queries");
	int i =0;
	foreach(QString q,slist){		
		sets.setArrayIndex(i++);
		sets.setValue("q",q);
	}
	sets.endArray();
}

void SavedQueriesDlg::accept()
{
	if(tableQueries->selectionModel()->selectedIndexes().count()>0)
		query = ((QStandardItemModel*)tableQueries->model())->itemFromIndex(tableQueries->selectionModel()->selectedIndexes().at(0))->text();
	QDialog::accept();
}

void SavedQueriesDlg::resizeEvent(QResizeEvent *s)
{
	//tableQueries->horizontalHeader()->setDefaultSectionSize(s->size().width()-45);
	
	
}
